package yu.code.yurpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Mock 服务代理（jdk动态代理）
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //根据方法的返回值类型，生成特定的默认值对象
        Class<?> returnType = method.getReturnType();
        log.info("mock invoke {}", method.getName());

        return getDefaultObject(returnType);
    }

    private Object getDefaultObject(Class<?> returnType) {
        //基本类型
        if (returnType.isPrimitive()) {
            if(returnType == boolean.class){
                return false;
            }else if(returnType == byte.class){
                return 0;
            }else if (returnType == char.class){
                return '\u0000';
            }else if (returnType == short.class){
                return (short)0;
            }else if (returnType == int.class){
                return 0;
            }else if (returnType == long.class){
                return 0L;
            }else if (returnType == float.class){
                return 0.0F;
            }else if (returnType == double.class){
                return 0.0D;
            }
        }
        //对象类型
        return null;
    }
}
